package com.android.plc.ui.control;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.android.plc.CONST;
import com.android.plc.CONST.COMMAND;
import com.android.plc.R;
import com.android.plc.util.PrefUtil;

public class HomeRemoteControl extends BaseControl implements OnClickListener
{
	private static final String TAG = "HomeRemoteControl";
	public ImageButton mOnBtn;
	public ImageButton mOffBtn;

	public Button mHomeBtn;
	public Button mRemoteBtn;

	public HomeRemoteControl(Context context, ViewGroup view)
	{
		super(context, view, R.layout.control_room);
	}

	@Override
	public void initView(ViewGroup view)
	{
		mOnBtn = (ImageButton) view.findViewById(R.id.btn_on);
		mOffBtn = (ImageButton) view.findViewById(R.id.btn_off);

		mHomeBtn = (Button) view.findViewById(R.id.btn_ctrl_home);
		mRemoteBtn = (Button) view.findViewById(R.id.btn_ctrl_remote);

		mOnBtn.setOnClickListener(this);
		mOffBtn.setOnClickListener(this);

		mHomeBtn.setOnClickListener(this);
		mRemoteBtn.setOnClickListener(this);

		SharedPreferences pref = PrefUtil.getPreferences(mContext);
		boolean home = pref.getBoolean(PrefUtil.PREF_HOME_CONTROL, true);
	
		mHomeBtn.setSelected(home);
		mRemoteBtn.setSelected(!home);

		mControlManager.setHomeCtrol(home);
	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_on:
			sendCommand(COMMAND.ALLUSERLTSON, CONST.DEF_DATA, 0);
			break;
		case R.id.btn_off:
			sendCommand(COMMAND.ALLUSERLTSOFF, 0, 0);
			break;
		case R.id.btn_ctrl_home:
			mHomeBtn.setSelected(true);
			mRemoteBtn.setSelected(false);
			mControlManager.setHomeCtrol(true);
			break;
		case R.id.btn_ctrl_remote:
			mHomeBtn.setSelected(false);
			mRemoteBtn.setSelected(true);
			mControlManager.setHomeCtrol(false);
			break;
		default:
			break;
		}
	}

	@Override
	public void onReceive(int command, int data1, int data2)
	{
		if (command == COMMAND.ALLUSERLTSON)
		{
			Log.w(TAG, "rev socket COMMAND.ALLUSERLTSON");
			mOnBtn.setAlpha(OFF_ALPHA);
			mOffBtn.setAlpha(ON_ALPHA);
		} else if (command == COMMAND.ALLUSERLTSOFF)
		{
			Log.w(TAG, "rev socket COMMAND.ALLUSERLTSOFF");
			mOnBtn.setAlpha(ON_ALPHA);
			mOffBtn.setAlpha(OFF_ALPHA);
		}
	}

}
